﻿#ifndef ELEMENTBASE_H
#define ELEMENTBASE_H

#include <QGraphicsItem>
#include <QVector>
#include <QLine>

class ElementBasePrivate;
class ElementBase :public QGraphicsObject
{
    Q_OBJECT
public:
    // 存储图形属性的数据
    class ElementUiBase{
        friend class ElementBase;
        friend class ElementUiBasePrivate;
        QVector<QLineF> mLines;
        QVector<QRectF> mRects;
        QVector<QPointF> mPoints;
        QVector<QRectF> mEllipses;
        QSizeF mSize;
    public:
        explicit ElementUiBase(){}
        virtual ~ElementUiBase(){}
        virtual bool fromJson(const QByteArray &json);
        bool fromFile(const QString& fileName);
    };
public:
    ElementBase(ElementUiBase* ui=nullptr);
    ElementBase(QGraphicsItem *parent = nullptr);
    ElementBase(const QSizeF&rect, ElementUiBase* ui=nullptr);

    ElementBase(QSizeF&&rect, ElementUiBase* ui=nullptr)
        :ElementBase(rect,ui){}
    virtual ~ElementBase();
    inline ElementUiBase *ui(){return mUI;}
    inline void setUi(ElementUiBase *ui){
        mUI=ui;
        //setShowRect(ui->mSize);
        update();
    }
    void setShowRect(const QSizeF&size);
    void setShowRect(qreal w,qreal h);
    inline QRectF showRect(){return mShowRect;}

    void addRotation(qreal arg);
    void verticalFlip();
    void horizontalFlip();

    QRectF boundingRect() const override final;

    //bool drawHover(){return mIsDrawHover;}
    //void setDrawHover(bool b){mIsDrawHover=b;}
signals:
    void selectedChange(bool);
    void clicked();
protected:
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
    void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
    //void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
    void draw(QPainter *painter,const QRectF& showRect) const;
    void drawHSpots(QPainter *painter,const QRectF& showRect) const;
    QPainterPath shape() const  override;;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
private:
    inline void drawHSpots(QPainter *painter,QRectF&& showRect) const
        {drawHSpots(painter,showRect);}
    ElementUiBase* mUI=nullptr;
    qreal mdCircleRadius = 5;
    qreal mdLineLen = 20;
    QRectF mShowRect {-50.,-50.,100.,100.};
    QColor mqcSelectedBorderColor {61,204,252};
    QPointF mPressPos;
    QString mToolTip="元素";
    bool mIsContains=false;
    //bool mIsDrawHover=true;
    // QPointF mPos={0.,0.};
    friend class ElementBasePrivate;
};

#endif // ELEMENTBASE_H
